define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'course/index' + location.search,
                    add_url: 'course/add',
                    edit_url: 'course/edit',
                    //del_url: 'course/del',
                    multi_url: 'course/multi',
                    import_url: 'course/import',
                    table: 'course'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('题库编号')},
                        {field: 'name', title: __('题库名称')},
                        {
                            field: 'type',
                            title: __('Type'),
                            formatter: Table.api.formatter.normal,
                            searchable:false,
                            searchList: {0: __('-'), 1: __('必考'), 2: __('选考 '), 3: __('加考 ')}
                        },
                        {field: 'code', title: __('题库代码')},
                        {field: 'extend_id', title: __('Extend_id')},
                        {
                            field: 'price',
                            title: __('Price'),
                            operate: 'BETWEEN',
                            searchable: false
                        },
                        {field: 'sale_num', title: __('题库销量')},
                        {
                            field: 'status',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {1: __('Normal'), 2: __('Hidden')}
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'add',
                                    text: __('题目分类管理'),
                                    classname: 'btn btn-primary btn-xs btn-addtabs',
                                    icon: 'fa fa-list-ul',
                                    url: 'questioncategory/index/course_id/{ids}'
                                },
                                {
                                name: 'view_papers',
                                text: __('题库资料管理'),
                                classname: 'btn btn-primary btn-xs btn-addtabs',
                                icon: 'fa fa-file-word-o',
                                url: 'material/file/index/course_id/{ids}'
                                }
                                ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 监听批量改价事件
            $('.btn-editprice').click(function (){
                var url = 'course/editprice';
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('批量改价'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 监听导入题库事件
            $('.btn-add-question').click(function (){
                var selectIds = Table.api.selectedids(table);
                var url = 'course/importQuestion/ids/'+selectIds.join(',');
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('导入题目'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 监听导入题库事件
            $('.btn-add-cate-question').click(function (){
                var selectIds = Table.api.selectedids(table);
                var url = 'course/importCateQuestion/ids/'+selectIds.join(',');
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('导入题目'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 监听导出题库事件
            $('.btn-export-question').click(function (){
                var selectIds = Table.api.selectedids(table);
                var url = 'course/exportQuestion/ids/'+selectIds.join(',');
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('导出题目'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        editprice: function (){
            Controller.api.bindevent();
        },
        importquestion:function (){
            $("#c-paper_id").data("params", function (obj) {
                return {custom: {course_id: $("#c-course_id").val()}};
            });
            Controller.api.bindevent();
        },
        importquestion:function (){
            $("#c-paper_id").data("params", function (obj) {
                return {custom: {course_id: $("#c-course_id").val()}};
            });
            Controller.api.bindevent();
        },
        importcatequestion:function (){
            $("#c-category_id").data("params", function (obj) {
                return {custom: {course_id: $("#c-course_id").val(),level:1}};
            });
            Controller.api.bindevent();
            Form.api.bindevent("form[role=form]", function(data, ret){
                window.location.href = data;
                return true;
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
